package buffbuildergui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JViewport;

/*
 * 
 * USED: http://tips4java.wordpress.com/2008/10/19/list-editor/
 *	A simple popup editor for a JList that allows you to change
 *  the value in the selected row.
 *
 *  The default implementation has a few limitations:
 *
 *  a) the JList must be using the DefaultListModel
 *  b) the data in the model is replaced with a String object
 *
 *  If you which to use a different model or different data then you must
 *  extend this class and:
 *
 *  a) invoke the setModelClass(...) method to specify the ListModel you need
 *  b) override the applyValueToModel(...) method to update the model
 */
public class EditListAction extends AbstractAction
{
	private JList list;

	private JPopupMenu editPopup;
	private JTextField editTextField;
	private Class<?> modelClass;
        private boolean isPlug;

	public EditListAction(boolean isPlug)
	{
		setModelClass(DefaultListModel.class);
                this.isPlug = isPlug;
	}

	protected void setModelClass(Class modelClass)
	{
		this.modelClass = modelClass;
	}

	protected void applyValueToModel(String value, ListModel model, int row)
	{
		DefaultListModel dlm = (DefaultListModel)model;
		dlm.set(row, value);
	}

	/*
	 *	Display the popup editor when requested
	 */
	public void actionPerformed(ActionEvent e)
	{
            list = (JList)e.getSource();
            ListModel model = list.getModel();
            DefaultListModel dlm = (DefaultListModel)model;
            
            if (! modelClass.isAssignableFrom(model.getClass())) return;
            
            int row = list.getSelectedIndex();
            JViewport jvp = (JViewport)list.getParent();
            JScrollPane jsp = (JScrollPane)jvp.getParent();
            EntityPanel ep = (EntityPanel)jsp.getParent();
            Entity en = ep.attachedEntity;
            
            Property curProp = ep.virtualList.get(row);
            String propertyName = curProp.getName();
            
            String input = "";
            if(curProp.getType() == Property.COUNT || curProp.getType() == Property.VOID)
                return;
            while(input.equals("")) {
                if(curProp.getType() == Property.FINITE) {
                    String dialog = "Select new value for property " + propertyName;
                    for(int i = 0; i < curProp.options.size(); i++) {
                        dialog = dialog + "\n[" + i + "] " + curProp.options.get(i);
                    }
                    JTextArea textArea = new JTextArea("Insert your Text here");
                    JScrollPane scrollPane = new JScrollPane(textArea);  
                    textArea.setLineWrap(true);  
                    textArea.setWrapStyleWord(true); 
                    scrollPane.setPreferredSize( new Dimension( 500, 500 ) );
                    textArea.setText(dialog);
                    input = JOptionPane.showInputDialog(scrollPane);
                    try {
                        int res = Integer.parseInt(input);
                        input = curProp.options.get(res);
                    }
                    catch(Exception error) {
                        if(input == null)
                            return;
                        else
                            input = "";
                    }
                }
                else
                {
                    input = JOptionPane.showInputDialog("Select new value for property " + propertyName,"");
                }
                if(input == null)
                    return;
            }
            if(curProp.set(input))
                ep.populate(dlm);
	}

	/*
	 *  Create the popup editor
	 */
	private void createEditPopup()
	{
		//  Use a text field as the editor

		editTextField = new JTextField();
		Border border = UIManager.getBorder("List.focusCellHighlightBorder");
		editTextField.setBorder( border );

		//  Add an Action to the text field to save the new value to the model

		editTextField.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				String value = editTextField.getText();
				ListModel model = list.getModel();
				int row = list.getSelectedIndex();
				applyValueToModel(value, model, row);
				editPopup.setVisible(false);
			}
		});

		//  Add the editor to the popup

	    editPopup = new JPopupMenu();
		editPopup.setBorder( new EmptyBorder(0, 0, 0, 0) );
    	editPopup.add(editTextField);
	}
}
